//
//  View++.swift
//  TLCLockDemo
//
//  Created by run on 2024/7/6.
//

import SwiftUI
import TLCLock

extension View {
    @ViewBuilder
    func yxrNavigationTitle(_ title: String) -> some View {
        if #available(iOS 14.0, *) {
            self.navigationTitle(title)
        } else {
            navigationBarTitle(title)
        }
    }

    @ViewBuilder
    func yxrSupportSelection() -> some View {
        if #available(iOS 15.0, *) {
            self.textSelection(.enabled)
        } else {
            self
        }
    }
}

#if canImport(UIKit)
    extension View {
        func hideKeyboard() {
            UIApplication.shared.sendAction(
                #selector(UIResponder.resignFirstResponder), to: nil, from: nil, for: nil
            )
        }
    }
#endif

extension View {
    func lockTask<T>(desc: String, task: @escaping () async -> (Result<T, TLCLockError>)) {
        Task {
            showLoading()
            let r = await task()
            hideLoading()
            if r.isFailure {
                showToast(r.errorMessage)
                return
            }
            showToast("\(desc)操作成功")
        }
    }
}
